package com.kc.web.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.kc.business.domain.BizUser;
import com.kc.business.domain.dto.BizBusinessDTO;
import com.kc.business.service.IBizGoodsOrderService;
import com.kc.business.service.IBizScenicBusinessService;
import com.kc.business.service.IBizScenicService;
import com.kc.business.service.IBizUserService;
import com.kc.business.service.impl.BizScenicServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.kc.common.constant.Constants;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.core.domain.entity.SysMenu;
import com.kc.common.core.domain.entity.SysUser;
import com.kc.common.core.domain.model.LoginBody;
import com.kc.common.core.domain.model.LoginUser;
import com.kc.common.utils.ServletUtils;
import com.kc.framework.web.service.SysLoginService;
import com.kc.framework.web.service.SysPermissionService;
import com.kc.framework.web.service.TokenService;
import com.kc.system.service.ISysMenuService;

import javax.annotation.Resource;

/**
 * 登录验证
 * 
 * @author kc
 */
@Slf4j
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;
    @Resource
    private IBizUserService bizUserService;
    @Resource
    private IBizScenicService bizScenicService;
    @Resource
    private IBizScenicBusinessService bizScenicBusinessService;
    @Resource
    private IBizGoodsOrderService iBizGoodsOrderService;



    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);

        //查询当前登录用户对应景区
        Map<String,Integer> map = new HashMap<>();
        BizUser bizUser = new BizUser();
        bizUser.setMobile(user.getPhonenumber());
        List<BizUser> bizUserList = bizUserService.getList(bizUser);
        if(!bizUserList.isEmpty()){
            BizUser user1 = bizUserList.get(0);
            if(user1.getIsScenic().equals(1)){
                //是景区角色，查询景区id
                Integer scenicId = bizScenicService.getInfoByUserId(user1.getId()).getId();
                map.put("scenicId",scenicId);
            }
            //获取当前用户商家列表
            if(user1.getIsBusiness().equals(1)){
                //是商家，获取商家列表数据
                List<BizBusinessDTO> bizBusinessDTOList = bizScenicBusinessService.getRuleBusinessListByUserId(user1.getId());
                //获取待处理事件数
                bizBusinessDTOList.forEach(bizBusinessDTO -> {
                    bizBusinessDTO.setEventNum(iBizGoodsOrderService.getNoRefund(bizBusinessDTO.getBusinessId(),0)+iBizGoodsOrderService.getNoSendOut(bizBusinessDTO.getBusinessId(),-bizBusinessDTO.getEntryType()));
                });
                ajax.put("businessList",bizBusinessDTOList);
            }
        }
        ajax.put("sysRuleId",map);


        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 用户信息
        SysUser user = loginUser.getUser();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
